package clientView;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Specialized;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import clientView.Spe_View.Mouse_tblSpe;
import database.SpecializedDelegate;

public class Spe_Insert  extends JDialog implements ActionListener{

	private JTextField txtSpeName;
	private JLabel lblSpeName;
	private JLabel lblSpeDes;
	private JTextArea txaSpeDes;
	private JScrollPane scrollPane;
	private JButton btnSave;
	private JButton btnCancel;

	/**
	 * Create the frame.
	 */
	public Spe_Insert (JFrame main) {
		super(main, "Thêm Chuyên Nghành", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		int height = 200;
	    int width = 450;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setFont(new Font("Times New Roman", Font.PLAIN, 13));
		setTitle("Thêm Hình Thức Hợp Đồng");
		setResizable(false);
	    initComponet();
	}
	    
	    public void initComponet()
	    {
		
	    GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.5, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblSpeName = new JLabel("Chuyên Nghành         :");
		lblSpeName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblSpeName = new GridBagConstraints();
		gbc_lblSpeName.anchor = GridBagConstraints.WEST;
		gbc_lblSpeName.insets = new Insets(15, 15, 5, 5);
		gbc_lblSpeName.fill = GridBagConstraints.VERTICAL;
		gbc_lblSpeName.gridx = 0;
		gbc_lblSpeName.gridy = 0;
		getContentPane().add(lblSpeName, gbc_lblSpeName);
		
		txtSpeName = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtSpeName, gbc_textField);
		txtSpeName.setColumns(10);
		txtSpeName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtSpeName.setText(StringUtil.removeUnwantedSpace(txtSpeName.getText()));
			}
		});
		//check database 
		txtSpeName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				SpecializedDelegate d = new SpecializedDelegate();
				if(!d.CheckInsertSpeName(StringUtil.removeUnwantedSpace(txtSpeName.getText()))&&txtSpeName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		lblSpeDes = new JLabel("Mô tả Chuyên Ngành :");
		lblSpeDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTDes = new GridBagConstraints();
		gbc_lblEmTDes.anchor = GridBagConstraints.WEST;
		gbc_lblEmTDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblEmTDes.fill = GridBagConstraints.VERTICAL;
		gbc_lblEmTDes.gridx = 0;
		gbc_lblEmTDes.gridy = 1;
		getContentPane().add(lblSpeDes, gbc_lblEmTDes);
		
		scrollPane = new JScrollPane();
		txaSpeDes = new JTextArea();
		txaSpeDes.setRows(4);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 2;
		gbc_scrollPane.insets = new Insets(5, 5, 5, 15);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(txaSpeDes);
		getContentPane().add(scrollPane, gbc_scrollPane);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
		
	
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			if(!StringUtil.isValidName(txtSpeName.getText(),200,2)){
				JOptionPane.showMessageDialog(null, "Tên Chuyên Nghành Ko để trống", 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
			}else{
				SpecializedDelegate c = new SpecializedDelegate();
				Specialized spe = new Specialized();
				spe.setSpecName(StringUtil.removeUnwantedSpace(txtSpeName.getText()));
				spe.setSpecDes(StringUtil.removeUnwantedSpace(txaSpeDes.getText()));
				boolean flag = c.insertSpe(spe, Main.userId);
				if(flag == true){
					this.setVisible(false);
					JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
					Spe_View.tblSpe.setVisible(false);
					Spe_View.tblSpe = UtilityGUI.getTable("Specialized", 1, "Specialized_Id", true);
					Spe_View.tblSpe.addMouseListener(new Mouse_tblSpe());
					Spe_View.scrollPane.setViewportView(Spe_View.tblSpe);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
							UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
			
		}else if(e.getSource() == btnCancel){
			dispose();
		}
		
	}
}
